import property = cc._decorator.property;
import ccclass = cc._decorator.ccclass;
import {utils} from "../utils/utils";
import {ResType} from "../config/config";
import {model} from "../web/model/Model";

@ccclass
export default class ShopItem extends cc.Component {
  @property(cc.Node)
  private selectedNode: cc.Node = null;
  @property(cc.Sprite)
  private icon: cc.Sprite = null;
  @property(cc.Node)
  private usedNode: cc.Node = null;

  onLoad() {
    this.selected = false;
    this.used = false;
    this.icon.node.scale = 0.8;
  }

  private _data: any = null;

  setData(data: any) {
    this._data = data;
    utils.setIcon(this.icon, data.id, ResType.skin);
    this.refreshUsed();
  }

  refreshUsed() {
    const data = this._data;
    this.used = false;
    if (data.type === ResType.skin) {
      if (model.user.activeSwim === data.id) {
        this.usedNode.active = true;
      }
    } else if (data.type === ResType.head) {
      if (model.user.activeRole === data.id) {
        this.usedNode.active = true;
      }
    }
  }

  getData() {
    return this._data;
  }

  get used(): boolean {
    return this.usedNode.active;
  }

  set used(value: boolean) {
    this.usedNode.active = value;
  }

  get selected(): boolean {
    return this.selectedNode.active;
  }

  set selected(value: boolean) {
    this.selectedNode.active = value;
  }
}